home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / clang / netclb23.zip / EXAMPLES.EXE / WHATHAS.C < prev   
C/C++ Source or Header  |  1994-05-20  |  11KB  |  322 lines

  1. /***************************************************************************/
  2. /* File:             WHATHAS.C                                             */
  3. /*                                                                         */
  4. /* Function:         List all files that are owned by the specified user.  */
  5. /*                                                                         */
  6. /* Usage:            whathas <username>                                    */
  7. /*                                                                         */
  8. /* Functions Called: GetBinderyObjectID                                    */
  9. /*                   GetVolumeName                                         */
  10. /*                   ScanDirectoryInformation                              */
  11. /*                   ScanFileInformation                                   */
  12. /*                   GetPreferredConnectionID                              */
  13. /*                   GetDefaultConnectionID                                */
  14. /*                   GetPrimaryConnectionID                                */
  15. /*                   SetPreferredConnectionID                              */
  16. /*                   ISShellLoaded                                         */
  17. /*                                                                         */
  18. /***************************************************************************/
  19.  
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <ctype.h>
  24. #ifndef TURBOC
  25. #include <malloc.h>
  26. #endif
  27.  
  28. #include "netware.h"
  29.  
  30. void usage ( void );
  31. void display_dir( char *this_dir );
  32. void do_files( char *this_dir,char *scandir );
  33. void do_directory( char *this_dir , int dirname_length );
  34.  
  35. char drive_letters[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ[/]^_,";
  36. char requested_user[256];
  37. long requserid;
  38. byte fileattr,extfileattr;
  39. long filesize;
  40. long fileowner;
  41. int sequence=-1;
  42. char fname[15];
  43. byte maxrightsmask;
  44. char dname[17];
  45. char ownername[49];
  46. word objecttype;
  47. int dir_len;
  48. long dirownerid;
  49.  
  50. char creationdate[4],lastaccessdate[4],lastupdatedatetime[7],
  51.      lastarchivedatetime[7],creationdatetime[7];
  52.  
  53. unsigned long totalfilesize,totalfiles;
  54.  
  55. char default_output[] = "whathas.txt";
  56. char output_name[256];
  57.  
  58. FILE *ofile;
  59.  
  60. void display_dir( char *this_dir )
  61. {
  62. char dir_to_show[81];
  63. char *d = this_dir;
  64.  
  65.      while (strlen(d) > 79)
  66.         d=(strchr(d,'\\')+1);
  67.      sprintf(dir_to_show,"%-79.79s\r",d);
  68.      printf(dir_to_show);
  69. }
  70.  
  71. void do_files( char *this_dir,char *scandir )
  72. {
  73. int dirshown = 0;
  74.  
  75. unsigned long loctotalfilesize=0,loctotalfiles=0;
  76.  
  77.      sequence = -1;
  78.      
  79.      while( ScanFileInformation( 0,scandir,6,&sequence,fname,
  80.                                  &fileattr,&extfileattr,
  81.                                  &filesize,creationdate,
  82.                                  lastaccessdate,lastupdatedatetime,
  83.                                  lastarchivedatetime,&fileowner ) == 0)
  84.      {
  85.         printf("%s\n",fname);
  86.         if (fileowner == requserid)
  87.         {
  88.             if (!dirshown)
  89.             {
  90.                 fprintf(ofile,"\ndirectory: %s\n\n",this_dir);
  91.                 dirshown = 1;
  92.             }
  93.             totalfiles++;
  94.             totalfilesize += filesize;
  95.             loctotalfiles++;
  96.             loctotalfilesize += filesize;
  97.             fprintf(ofile,"      %-13.13s    %10ld\n",fname,filesize);
  98.         }
  99.      }
  100.      if (loctotalfiles)
  101.      {
  102.          fprintf(ofile,"                       ==========\n");
  103.          fprintf(ofile,"             Total:    %10ld  (%ld files)\n",
  104.                        loctotalfilesize,loctotalfiles);
  105.          fprintf(ofile,"                       ==========\n");
  106.      }
  107. }
  108.  
  109. void do_directory( char *this_dir , int dirname_length )
  110. {
  111. int rcode=0;
  112. int subdirno=0;
  113. char *scandir;
  114. char *nextdir;
  115.  
  116. /* This procedure is called recursively in order to process every directory */
  117. /* and subdirectory found. Therefore we use malloc as much as possible in   */
  118. /* order to allocate required storage, this will stop us blowing the stack. */
  119.  
  120.    scandir = (char *)malloc(dirname_length+3); /* Get enough space to store */
  121.                                                /* current directory name +  */
  122.                                                /* '\*' + terminating NULL   */
  123.  
  124.    strcpy(scandir,this_dir);                   /* Construct scan dir path   */
  125.    strcpy(scandir+dirname_length,"\\*");
  126.  
  127.    display_dir( this_dir );                    /* Display directory name    */
  128.                                                /* on console                */
  129.    
  130.    do_files( this_dir,scandir );               /* Display files in this     */
  131.                                                /* directory                 */
  132.  
  133.    while ( rcode == 0 )      /* Now get all sub_directories */
  134.    {
  135.       rcode = ScanDirectoryInformation( 0,scandir,&subdirno,
  136.                                         dname,(byte *)creationdatetime,
  137.                                         &dirownerid,
  138.                                         &maxrightsmask);
  139.       if (rcode == 0)
  140.       {
  141.          dir_len = strlen(dname);
  142.          
  143.          /* Get enough space to store full path name of found sub_directory */
  144.          
  145.          nextdir = (char *)malloc(dirname_length+dir_len+2);
  146.  
  147.          strcpy(nextdir,this_dir);            /* Construct full path name */
  148.          *(nextdir+dirname_length) = '\\';
  149.          strcpy(nextdir+dirname_length+1,dname);
  150.  
  151.          do_directory(nextdir,dirname_length + 1 + dir_len); /* do it !! */
  152.          
  153.          free(nextdir);
  154.       }
  155.    }
  156.    free(scandir);
  157. }
  158.  
  159. void usage ( void )
  160. {
  161.    printf("\nWhathas - (c) Adrian M. Cunnelly 1993\n\n");
  162.    printf("        Scan all directories on all volumes");
  163.    printf(" attached to the current server\n");
  164.    printf("        and produce a listing of all files owned by the");
  165.    printf(" specified Netware User.\n\n");
  166.    printf("        Usage: Whathas <switches> Netware_User\n\n");
  167.    printf("               Switches:\n");
  168.    printf("                        -ofile_name - Produce output to 'file_name'\n");
  169.    printf("                                      (default WHATHAS.TXT)\n");
  170.    printf("\n");
  171. }
  172.  
  173. void main(int argc,char *argv[])
  174. {
  175. char volume[17];
  176. int this_one;
  177. char *this_param;
  178. int param=1;
  179. int rcode;
  180. char *o_name = default_output;   /* Set output name to default */
  181. char underline[80];
  182. int prefserver;
  183. int thisserver;
  184.  
  185.    totalfilesize = totalfiles = 0;
  186.  
  187.    requested_user[0]='\0';
  188.    output_name[0] = '\0';
  189.  
  190.    if (IsShellLoaded() != SUCCESS)
  191.    {
  192.       printf("*** No netware shell loaded ***\n");
  193.       exit(255);
  194.    }
  195.    else
  196.    if ((prefserver = GetPreferredConnectionID()) == 0)
  197.    {
  198.       if ((thisserver = GetDefaultConnectionID()) == 0)
  199.          thisserver = GetPrimaryConnectionID();
  200.       SetPreferredConnectionID( thisserver );
  201.    }
  202.    else
  203.       thisserver = prefserver;
  204.  
  205.    if (argc < 2)       /* Not enough parameters */
  206.    {
  207.       usage();
  208.       if (thisserver != prefserver)   /* reset preferred server */
  209.          SetPreferredConnectionID( prefserver );
  210.       exit(0);
  211.    }
  212.  
  213.    while(--argc)
  214.    {
  215.       this_param = argv[param++];
  216.  
  217.       if(*this_param == '-' || *this_param == '/')
  218.       {
  219.          switch(toupper(*(++this_param)))
  220.          {
  221.              case '?':
  222.                       usage();
  223.                       if (thisserver != prefserver)
  224.                          SetPreferredConnectionID( prefserver );
  225.                       exit(0);
  226.                       break;
  227.              case 'O':
  228.                       if (output_name[0])
  229.                       {
  230.                          printf("Output name already specified\n");
  231.                          if (thisserver != prefserver)
  232.                             SetPreferredConnectionID( prefserver );
  233.                          exit(255);
  234.                       }
  235.                       else
  236.                       {
  237.                          strcpy(output_name,this_param+1);
  238.                          o_name = output_name;
  239.                       }
  240.                       break;
  241.              default:
  242.                       printf("Invalid switch specified\n\n");
  243.                       usage();
  244.                       if (thisserver != prefserver)
  245.                          SetPreferredConnectionID( prefserver );
  246.                       exit(255);
  247.          }
  248.       }
  249.       else
  250.       if (requested_user[0])
  251.       {
  252.          printf("Username already specified\n");
  253.          if (thisserver != prefserver)
  254.             SetPreferredConnectionID( prefserver );
  255.          exit(255);
  256.       }
  257.       else
  258.       {
  259.          strcpy(requested_user,this_param);
  260.          strupr(requested_user);
  261.          if ((rcode=GetBinderyObjectID(USER,requested_user,&requserid)) != 0)
  262.          {
  263.             printf("\nUser: %s - not found\n",requested_user);
  264.             if (thisserver != prefserver)
  265.                SetPreferredConnectionID( prefserver );
  266.             exit(255);
  267.          }
  268.       }
  269.    }
  270.  
  271.    if (!requested_user[0])
  272.    {
  273.       printf("A username must be specified\n\n");
  274.       usage();
  275.       if (thisserver != prefserver)
  276.           SetPreferredConnectionID( prefserver );
  277.       exit(255);
  278.    }
  279.  
  280.    strupr(o_name);    /* Convert output filename to uppercase */
  281.    
  282.    if ((ofile = fopen(o_name,"w")) == NULL)
  283.    {
  284.       printf("Failed to open output file: %s\n",o_name);
  285.       if (thisserver != prefserver)
  286.           SetPreferredConnectionID( prefserver );
  287.       exit(255);
  288.    }
  289.  
  290.    /* Write headings to output file */
  291.    
  292.    fprintf(ofile,"\fFiles owned by Netware user %s\n",requested_user);
  293.    fputs(          "============================",ofile );
  294.    memset((void *)&underline,'\0',80);
  295.    memset((void *)&underline,'=',strlen(requested_user));
  296.    fputs(underline,ofile);
  297.    fputc('\n',ofile);
  298.  
  299.    /* Loop round all volumes */
  300.  
  301.    for(this_one=0;this_one<31;this_one++)
  302.        if ((GetVolumeName(this_one,volume) == 0) && (volume[0]))
  303.        {
  304.           strcat(volume,":");
  305.           do_directory(volume,strlen(volume));
  306.        }
  307.        
  308.    display_dir(" ");          /* Clear display line on console */
  309.    
  310.    fprintf(ofile,"\n%s has:\n",requested_user);
  311.    fprintf(ofile,"    %lu bytes in %lu files\n",totalfilesize,totalfiles);
  312.  
  313.    fclose(ofile);
  314.  
  315.    printf("File: %s - produced OK for User: %s\n",o_name,requested_user);
  316.  
  317.    if (thisserver != prefserver)
  318.       SetPreferredConnectionID( prefserver );
  319.  
  320.    exit(0);
  321. }
  322.